package com.training1.core.web.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.global.signIn.LoginInfo;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.BatchWithDrawalService;
import com.training1.core.modules.service.MemberService;

@Controller
@RequestMapping("audit")
public class AuditController extends BaseController {
	@Resource
	private BatchWithDrawalService service;
	@Resource
	private MemberService service2;

	@RequestMapping({ "/", "" })
	public String auditSet() {
		return "yixiang/auditSetting";
	}

	@RequestMapping("list")
	@ResponseBody
	public Page<BatchWithDrawalEntity> list(String cellphone, String createTimeFrom, String createTimeTo,
			String withdrawCashFrom, String withdrawCashTo, Page<BatchWithDrawalEntity> page) {
		Map<String,Object> map = new HashMap<String,Object>();
		page.getSearch().put("cellphone", cellphone);
		map.put("cellphone", cellphone);
		if (createTimeFrom != null) {
			page.getSearch().put("createTimeFrom", createTimeFrom + " 00:00:00");
			map.put("createTimeFrom", createTimeFrom + " 00:00:00");
		}
		if (createTimeTo != null) {
			page.getSearch().put("createTimeTo", createTimeTo + " 23:59:59");
			map.put("createTimeTo", createTimeTo + " 23:59:59");
		}
			page.getSearch().put("withdrawCashFrom", withdrawCashFrom);
			page.getSearch().put("withdrawCashTo", withdrawCashTo);
			map.put("withdrawCashFrom", withdrawCashFrom);
			map.put("withdrawCashTo", withdrawCashTo);
		service.findList(page.getSearch());
		//查询金额总和
		BatchWithDrawalEntity bde = service.findAllMoney(map);
		if(bde==null){
			bde = new BatchWithDrawalEntity();
		}
		bde.setBankforCard("总合计：");
		List<BatchWithDrawalEntity> cashes = new ArrayList<BatchWithDrawalEntity>();
		
		cashes.add(bde);
		page.setFooter(cashes);

		return page;
	}

	@RequestMapping(value = "/auditSuccess", method = RequestMethod.POST)
	@ResponseBody
	public RespJson auditSuccess(String orderNumber, String flg) {
		LoginInfo info = LoginInfoUtil.getLoginInfo();
		String updateName = info.getLoginName();
		Map<String, Object> map = saveMap(orderNumber, updateName);
		if ("1".equals(flg)) {
			map.put("flg", flg);
			service.updateStatus(map);
		}
		if ("2".equals(flg)) {
			map.put("flg", flg);
			//通过订单编号查出提现会员提现表中信息
			List<BatchWithDrawalEntity> list = service.findByOrderNumber(map);
			//定义list集合存放查出的会员手机号
			List<String> list1 = new ArrayList<String>();
			//定义map集合以手机号为键，提现金额为值存放
			Map<String,Object> map1 = new HashMap<String,Object>();
			for (BatchWithDrawalEntity batchWithDrawalEntity : list) {
				list1.add(batchWithDrawalEntity.getPhone());
				map1.put(batchWithDrawalEntity.getPhone(), batchWithDrawalEntity.getMoney());
			}
			//通过手机号查出提现会员会员表中信息
			List<MemberEntity> memList = service2.findBatch(list1);
			//以手机号为键，对应的member对象为值存放
			Map<String,Object> map2 = new HashMap<String,Object>();
			for (MemberEntity mbe : memList) {
				map2.put(mbe.getCellPhone(), mbe);
			}
			//定义新list集合存放新的member对象
			List<MemberEntity> memList1 = new ArrayList<MemberEntity>();
			//将map1中的键取出，通过map1中的键去map2中取对象
			Object[] obj = map1.keySet().toArray();
			for(int i=0;i<map1.size();i++){
				MemberEntity mem = (MemberEntity) map2.get(obj[i]);
				double money = (double) map1.get(obj[i]);
				mem.setAvaibleCash(new BigDecimal(mem.getAvaibleCash().doubleValue()+money));
				memList1.add(mem);
			}
			service.updateStatus1(map,memList1);
		}
		
		return RespJsonFactory.buildSuccess();

	}

	// 审核时将数据存到map
	public Map<String, Object> saveMap(String orderNumber, String updateName) {
		//将orderNumber按，切割成数组
		String[] str = orderNumber.split(",");
		//定义map集合用来存放orderNumber
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		if (str.length == 1) {
			list.add(orderNumber);
			map.put("orderNumber", list);
			map.put("auditor", updateName);
		} else {

			for (int i = 0; i < str.length; i++) {
				list.add(str[i]);
			}
			map.put("orderNumber", list);
			map.put("auditor", updateName);
		}
		return map;
	}
	
	@RequestMapping("/export")
	public String outExcel(String cellphone, String createTimeFrom, String createTimeTo, String withdrawCashFrom,
			String withdrawCashTo, HttpServletResponse resp) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("cellphone", cellphone);
		if(createTimeFrom!=null){
			map.put("createTimeFrom", createTimeFrom+" 00:00:00");
		}
		if(createTimeTo!=null){
			map.put("createTimeTo", createTimeTo+" 23:59:59");
		}
		map.put("withdrawCashFrom", withdrawCashFrom);
		map.put("withdrawCashTo", withdrawCashTo);
		//根据条件查询
		List<BatchWithDrawalEntity> list = service.findList(map);
		service.export(list,resp);
		return null;
	}
}
